<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

class ActionController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'action'],
        'show' => ['get', 'action/%d'],
        'store' => ['post', 'action'],
        'update' => ['put', 'action/%d'],
        'destroy' => ['delete', 'action/%d'],
        'all' => ['get', 'action/all']
    ];

    public function index(Request $request)
    {
        $res = http_admin($this->api_url['index'])->send(['page' => $request->input('page',1)]);
        $data = paginate($request, $res['data']);
        return view('Admin.action.index',$data);
    }
    
    public function edit($id){
        $res = http_admin($this->api_url['show'], [$id])->send();
        $data = $res['data'];
        $res = http_admin($this->api_url['all'])->send();
        $actions = array_get($res,'data.list');//所有操作
        $groups = [];
        foreach($actions as $act){
            $groups[] = array_get(explode('.',$act),'1');
        }
        $groups = array_unique($groups);//分组
        $data['actions'] = $actions;
        $data['groups'] = $groups;
        return view('Admin.action.edit',$data);
    }


    public function create(){
        $res = http_admin($this->api_url['all'])->send();
        $actions = array_get($res,'data.list');//所有操作
        $groups = [];
        foreach($actions as $act){
           $groups[] = array_get(explode('.',$act),'1'); 
        }
        $groups = array_unique($groups);//分组
        return view('Admin.action.create',compact('actions','groups'));
    }

    public function store(Request $request)
    {
        $data = $request->except('_token','_method');
        $res = http_admin($this->api_url['store'])->send($data);
        return redirect('/foradmin/action');
    }

    public function update($id,Request $request)
    {
        $data = $request->except('_token','_method');
        $res = http_admin($this->api_url['update'], [$id])->send($data);
        return redirect('/foradmin/action');
        
    }

    public function destroy($id)
    {
        $res = http_admin($this->api_url['destroy'], [$id])->send();
        return redirect('/foradmin/action');
    }
}
